//
//  AboutUsViewController.m
//  LYZhiYuanDriver
//
//  Created by mac on 2020/1/2.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "AboutUsViewController.h"
#import "CYWebViewViewController.h"

@interface AboutUsViewController ()

@property (weak, nonatomic) IBOutlet UILabel *appNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *appLogo;
@property (weak, nonatomic) IBOutlet UILabel *copyrightLabel;

@property (nonatomic, strong) NSArray *dataArr;
@property (weak, nonatomic) IBOutlet UILabel *vLine;
@property (weak, nonatomic) IBOutlet UIButton *userXieYiBtn;
@property (weak, nonatomic) IBOutlet UIButton *yinSiXieYiBtn;

@end

@implementation AboutUsViewController

static const CGFloat itemHeight = 45;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = BackgroundColor;
    
    self.navigationItem.title = @"关于我们";
    
    _appNameLabel.textColor = Dark_TextColor;
    
    _copyrightLabel.textColor = Dark_TextColor;
    _copyrightLabel.numberOfLines = 2;
    
    cut(_appLogo, 5);
    
#ifdef isProductionEnvironment
    
#else
    _appLogo.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionLongPress:)];
    // 最短长按时间
    longPress.minimumPressDuration = 2;
    [_appLogo addGestureRecognizer:longPress];
#endif
    
    _vLine.textColor = Theme_ColorValue;
    [_userXieYiBtn setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
    [_yinSiXieYiBtn setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], PARTYGROUP_ABOUTUS, [UserDefaultUtil valueForKey:BranchCode]) withParameter:@{@"token":[UserDefaultUtil valueForKey:USER_TOKEN],} withVC:self netName:@"关于我们" success:^(id response, NSString *msg) {
        
        [_appLogo sd_setImageWithURL:[NSURL URLWithString:response[@"driverLogoPath"]]];
        
        _appNameLabel.text = response[@"driverAppName"];
        
        _dataArr = @[ response[@"mobilePhone"],response[@"wwwUrl"], @""];
        
        //版权信息
        _copyrightLabel.text = CJStringWithFormat(@"%@\n%@", response[@"ownerName"], response[@"copyrightName"]);
        
        for (int i=0; i<3; i++) {
            UILabel *label = [self.view viewWithTag:100+i];
            label.text = _dataArr[i];
        }
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
    
    
    NSArray *titleArr = @[@"服务热线"];
    NSArray *valueArr = @[@"-", @"-"];
    
    for (int i=0; i<titleArr.count; i++) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, _appNameLabel.bottom + itemHeight*i + 30, ScreenW, itemHeight)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.userInteractionEnabled = YES;
        [self.view addSubview:bgView];
        
        [bgView nyl_addTapGes:^{
//            if (i==1) {
                
                CALL_PHONE(_dataArr[i])
                
//            } else if (i==0) {
//                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_dataArr[i]]];
//
//            }
        }];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, itemHeight)];
        textLabel.text = titleArr[i];
        textLabel.textColor = Dark_TextColor;
        textLabel.font = font(15);
        textLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:textLabel];
        
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenW-230, 0, 200, itemHeight)];
        valueLabel.text = valueArr[i];
        valueLabel.tag = 100 + i;
        valueLabel.textColor = Light_TextColor;
        valueLabel.textAlignment = NSTextAlignmentRight;
        valueLabel.font = font(15);
        valueLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:valueLabel];
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenW-20, 17, 6, 11)];
        arrow.image = image(@"gcy_icon_hd_rarrow");
        [bgView addSubview:arrow];
        
        if (i<2) {
            //分割线
            UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, itemHeight-1, ScreenW - 30, 1)];
            hLine.backgroundColor = BackgroundColor;
            [bgView addSubview:hLine];
        }
    }
}

- (void)actionLongPress:(UILongPressGestureRecognizer *)longPress {
    // 根据手势的状态判断，只有第一次执行时是UIGestureRecognizerStateBegan，其余都是UIGestureRecognizerStateEnd状态。
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        //提示框添加文本输入框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"测试人员专用"
                                                                       message:@"请填写ip/http地址，保存后重启APP。"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
            //响应事件
            //得到文本信息
            for(UITextField *text in alert.textFields){
                DebugLog(@"text = %@", text.text);
                
                [ReAddressDynamic shared].baseUrl = text.text;
                
                [UserDefaultUtil saveValue:text.text forKey:@"BASEREADDRESS"];
            }
        }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * action) {
            
        }];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            
            if ([UserDefaultUtil valueForKey:@"BASEREADDRESS"] == NULL) {
                // textField.text = @"http://";
                textField.text = BasicDomainURL;
            } else {
                textField.text = [UserDefaultUtil valueForKey:@"BASEREADDRESS"];
            }
            
            textField.keyboardType = UIKeyboardTypeURL;
        }];
        
        
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (IBAction)xieYiTypeClick:(UIButton *)sender {
    
    CYWebViewViewController *webVC = [[CYWebViewViewController alloc] init];
    if (sender.tag == 10) {
        webVC.titleString = @"服务协议";
        webVC.urlString = CJStringWithFormat(@"%@/agreement/agreement.html?agreementType=2", BasicDomainURL);
        
    } else {
        webVC.titleString = @"隐私协议";
        webVC.urlString = CJStringWithFormat(@"%@/agreement/agreement.html?agreementType=1", BasicDomainURL);
        
    }
    
    webVC.state = 1;
    [self.navigationController pushViewController:webVC animated:YES];
    
}


@end
